﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using WPFExcelReport.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class HWDWSON : DocumentBase
    {
        #region-------------Properties-----------------------

        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI;
        HWDWSONData m_Data, m_DataX;
        public DataSet setResult;
        private DataRow dwUI;
        HWDWSONReport m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataRow dwBSFARB, dwWASTAMM, dwBHSATZ;
        string UserID = "";
        string ART = "";
        DataRow dwBASTGA, dwBASTKE, dwWASTGA, dwWTGEW, dwWASTPASS, dwWASTKE;
        CollectionViewSource vwWDECK, vwVKPOS, vwBSSATZ, vwBHSATZ, vwBSFARB, vwWASTAMM, vwWASTKE,
            vwWTGEW, vwDRAFTING, vwWASTPASS, vwWTPASS, vwWABIND, vwWASTGA, vwBASTGA, vwUIABPOS, vwWPGAAUF, vwWFGAAUF;
        DataRow dwBSSATZ;
        DataTable tbGSDAT;
        DataTable tbWTSAER, tbWTSCHUSS, tbWABIND, tbWTEINZ, tbWTPASS, tbWADART;
        bool isWarpYarn = true;
        DataSet setDISPO = new DataSet();
        DataSet setUIABPOS = new DataSet();
        DataSet setVKPOS = new DataSet();
        DataSet setENV = new DataSet();
        DataSet setWEAVE = new DataSet();
        DataSet setUIWAART = new DataSet();
        DataRow dwSetDispo = null;
        DataTable tbDRAFTING = new DataTable();
        DataTable tbPEGPLAN = new DataTable();
        BackgroundWorker bgWorker, bgWorkerOn;
        MessageInfo msg;
        AB05ONData m_AB05;
        GreigeMasterData m_GM;
        DataTable tbUIABPOS, tbWFGAAUF, tbWPGAAUF, tbUIWAART;
        private string color
        {
            get
            {
                return m_GM.Left(m_GM.ConvertGB2312To8859P1(txtWAFZUSATZ.Text) + "                ", 10);
            }
        }
        private string finish
        {
            get
            {
                return m_GM.Left(m_GM.ConvertGB2312To8859P1(txtFINISH.Text) + "                ", 10);
            }
        }
        private DataRow dwWAFARBEN
        {
            get
            {
                DataRow row = null;
                var query = from dwTemp in setUI.WAFARBEN.AsEnumerable()
                            where dwTemp["WAFZUART"].ToString() == "C"
                            && dwTemp["WAFZUSATZ"].ToString().Trim() == color.Trim()
                            select dwTemp;
                foreach (DataRow dwTemp in query)
                {
                    row = dwTemp;
                }
                return row;
            }
        }
        public HWDWSON()
        {
            InitializeComponent();
            this.Name = "织造工艺设计";
            this.Title = "织造工艺设计";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI = (INTEXDataSet)TryFindResource("iNTEXDataSet");
                dwUI = tbUIWAART.NewRow();
                bgWorker = new BackgroundWorker();
                bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
                bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
                bgWorkerOn = new BackgroundWorker();
                bgWorkerOn.DoWork += new DoWorkEventHandler(bgWorkerOn_DoWork);
                bgWorkerOn.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorkerOn_RunWorkerCompleted);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            { 
                EventDispatcher.Instance.RaiseStatus("环境准备好", StatusEventType.StopProgress);
                BlockUI(19, true);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void BlockUI(int blocklevel, bool enable = true)
        {
            try
            {
                switch (blocklevel)
                {
                    case 10://before login
                        this.txtLogIn.Text = "请先登录系统";
                        txtLogIn.Background = new SolidColorBrush(Colors.Red);
                        btnGetVKPOS.IsEnabled = false;
                        btnQuery.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        znWS.Header = "未知用户信息";
                        znWS.IsExpanded = false;
                        znBS.IsExpanded = false;
                        btnGetVKPOS.IsEnabled = false;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = false;
                        btnQuery.IsEnabled = false;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 19://after login
                        expLog.IsExpanded = false;
                        expLog.Visibility = Visibility.Hidden;  
                        znWS.Header = "技术数据";
                        znWS.IsExpanded = true;
                        znBS.IsExpanded = false;
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = false;
                        btnQuery.IsEnabled = false;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 20:// before query VK
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = false;
                        btnQuery.IsEnabled = false;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 29:// after have VK
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = true;
                        btnQuery.IsEnabled = false;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 30://before UIWAART
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = true;
                        btnQuery.IsEnabled = false;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 39://AfterUIAART
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = true;
                        btnQuery.IsEnabled = true;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 40: //before WASTAMM
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = false;
                        btnInsertABON.IsEnabled = false;
                        btnRefreshUIWAART.IsEnabled = true;
                        btnQuery.IsEnabled = true;
                        btnReport.IsEnabled = false;
                        btnPrintPreview.IsEnabled = false;
                        btnSaveWASTAMM.IsEnabled = false;
                        btnNewWASTKE.IsEnabled = false;
                        btnNewWYarn.IsEnabled = false;
                        btnSaveWYarn.IsEnabled = false;
                        btnNewFYarn.IsEnabled = false;
                        btnSaveFYarn.IsEnabled = false;
                        btnQueryWEAVE.IsEnabled = false;
                        btnSaveWEAVE.IsEnabled = false;
                        break;
                    case 49://after WASTAMM
                        btnGetVKPOS.IsEnabled = true;
                        btnRefreshABON.IsEnabled = true;
                        btnInsertABON.IsEnabled = true;
                        btnRefreshUIWAART.IsEnabled = true;
                        btnQuery.IsEnabled = true;
                        btnReport.IsEnabled = true;
                        btnPrintPreview.IsEnabled = true;
                        btnSaveWASTAMM.IsEnabled = true;
                        btnNewWASTKE.IsEnabled = true;
                        btnNewWYarn.IsEnabled = true;
                        btnSaveWYarn.IsEnabled = true;
                        btnNewFYarn.IsEnabled = true;
                        btnSaveFYarn.IsEnabled = true;
                        btnQueryWEAVE.IsEnabled = true;
                        btnSaveWEAVE.IsEnabled = true;
                        break; 
                    default:
                        break;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {

                tbDRAFTING = setUI.DRAFTING;
                tbPEGPLAN = setUI.PEGPLAN;
                tbGSDAT = m_Data.ListGSDAT().Tables["GSDAT"];

                tbWTSAER = m_Data.ListWTSAER().Tables["WTSAER"];

                tbWTSCHUSS = m_Data.ListWTSCHUSS().Tables["WTSCHUSS"];

                tbWABIND = m_Data.GetRWABIND().Tables["WABIND"];

                tbWTEINZ = m_Data.GetRWTEINZ().Tables["WTEINZ"];

                tbWTPASS = m_Data.GetRWTPASS().Tables["WTPASS"];

                tbWADART = m_Data.GetRWADART().Tables["WADART"];

                setENV.Merge(tbGSDAT);
                setENV.Merge(tbWTSAER);
                setENV.Merge(tbWTSCHUSS);
                setENV.Merge(tbWABIND);
                setENV.Merge(tbWTEINZ);
                setENV.Merge(tbWTPASS);
                setENV.Merge(tbWADART);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                setUI = (INTEXDataSet)TryFindResource("iNTEXDataSet");
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                    OracleDB = (OracleConnection)dicParamater["OracleDB"];
                    proxy = (IntexReptServBLClient)dicParamater["proxy"];
                    m_Data = new HWDWSONData(OracleDB, proxy);
                    BlockUI(10, false);
                    #region============TESTINGDATA================

                    txtART.Text = "PT32A32A1201001053";
                    txtVAR.Text = "  ";
                    txtART.Text = "105-85-STX2104";
                    txtVAR.Text = "1 ";
                    txtTTENDS.Text = "5000";
                    txtNOBEAM.Text = "8";
                    txtVKPAUFKRS.Text = "C0";
                    txtVKPAUFLFD.Text = "100227";
                    txtVKPAUFPOS.Text = "001";
                    txtVKPAUFKRS.Text = "C1";
                    txtVKPAUFLFD.Text = "102104";
                    txtVKPAUFPOS.Text = "001";
                    txtUserID.Text = (string)dicParamater["User"];
                    txtPassword.Password = (string)dicParamater["Pwd"];
                    #endregion
                    #region============SCREEN====================
                    vwBSSATZ = (CollectionViewSource)this.FindResource("vwBSSATZ");
                    vwBHSATZ = (CollectionViewSource)this.FindResource("vwBHSATZ");
                    vwBSFARB = (CollectionViewSource)this.FindResource("vwBSFARB");
                    vwWASTAMM = (CollectionViewSource)this.FindResource("vwWASTAMM");
                    vwWTGEW = (CollectionViewSource)this.FindResource("vwWTGEW");
                    vwWASTPASS = (CollectionViewSource)this.FindResource("vwWASTPASS");
                    vwWABIND = (CollectionViewSource)this.FindResource("vwWABIND");
                    vwWASTGA = (CollectionViewSource)this.FindResource("vwWASTGA");
                    vwBASTGA = (CollectionViewSource)this.FindResource("vwBASTGA");
                    vwVKPOS = (CollectionViewSource)this.FindResource("vwVKPOS");
                    vwWDECK = (CollectionViewSource)this.FindResource("vwWDECK");
                    vwWASTKE = (CollectionViewSource)this.FindResource("vwWASTKE");
                    vwDRAFTING = (CollectionViewSource)this.FindResource("vwDRAFTING");
                    vwUIABPOS = (CollectionViewSource)this.FindResource("vwUIABPOS");
                    vwWPGAAUF = (CollectionViewSource)this.FindResource("vwWPGAAUF");
                    vwWFGAAUF = (CollectionViewSource)this.FindResource("vwWFGAAUF");

                    setWEAVE = m_Data.LoadSetWEAVE();
                    setDISPO = m_Data.LoadSetDispo(txtSetDispo.Text.Trim());
                    tbUIABPOS = new DataTable();
                    tbUIABPOS.TableName = "UIABPOS";
                    tbUIABPOS.Columns.Add("ORDTYPE", typeof(string));
                    tbUIABPOS.Columns.Add("ORDNUM", typeof(string));
                    tbUIABPOS.Columns.Add("QUANTITY", typeof(string));
                    tbUIABPOS.Columns.Add("UNIT", typeof(string));
                    tbUIABPOS.Columns.Add("UM", typeof(string));
                    tbUIABPOS.Columns.Add("DELIVERYDATE", typeof(string));
                    tbUIABPOS.Columns.Add("LEADTIME", typeof(string));
                    setUIABPOS.Merge(tbUIABPOS);
                    tbWFGAAUF = new DataTable();
                    tbWFGAAUF.TableName = "WFGAAUF";
                    tbWFGAAUF.Columns.Add("YARN", typeof(string));
                    tbWFGAAUF.Columns.Add("QUANTITY", typeof(string));
                    tbWFGAAUF.Columns.Add("UM", typeof(string));
                    tbWPGAAUF = new DataTable();
                    tbWPGAAUF.TableName = "WPGAAUF";
                    tbWPGAAUF.Columns.Add("YARN", typeof(string));
                    tbWPGAAUF.Columns.Add("QUANTITY", typeof(string));
                    tbWPGAAUF.Columns.Add("UM", typeof(string));
                    tbUIWAART = new DataTable();
                    tbUIWAART.TableName = "UIWAART";
                    tbUIWAART.Columns.Add("WAART", typeof(string));
                    tbUIWAART.Columns.Add("WAVAR", typeof(string));
                    tbUIWAART.Columns.Add("WAFART", typeof(string));
                    tbUIWAART.Columns.Add("WAFVAR", typeof(string));
                    tbUIWAART.Columns.Add("WAFZUSATZ", typeof(string));
                    tbUIWAART.Columns.Add("BSFKART", typeof(string));
                    tbUIWAART.Columns.Add("BSFKVAR", typeof(string));
                    tbUIWAART.Columns.Add("BSFKFARBE", typeof(string));
                    tbUIWAART.Columns.Add("WANEUB", typeof(string));
                    tbUIWAART.Columns.Add("WANEUD", typeof(string));
                    setUIABPOS.Merge(tbUIABPOS);
                    setUIABPOS.Merge(tbWFGAAUF);
                    setUIABPOS.Merge(tbWPGAAUF);
                    setUIWAART.Merge(tbUIWAART);
                    dlgUIABPOS.DataContext = setUIABPOS;
                    dlgWFGAAUF.DataContext = setUIABPOS;
                    dlgWPGAAUF.DataContext = setUIABPOS;
                    dlgUIWAART.DataContext = setUIWAART;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count > 0)
            {
                DataRow dwUser = setResult.Tables[0].Rows[0];
                txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
                UserID = userID;
                m_Data.UserID = userID;
                this.UserID = userID;

                m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                    + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
                m_Data.MANDANT = Mandant;
                m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "已登录";
                txtLogIn.Background = new SolidColorBrush(Colors.Green);
                m_AB05 = new AB05ONData(OracleDB, proxy, UserID);
                m_GM = new GreigeMasterData(OracleDB, proxy, UserID);
                #region============SETUPDATA====================
                EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
                EventDispatcher.Instance.RaiseStatus("开始读取环境变量", StatusEventType.StartProgress);
                bgWorker.RunWorkerAsync();
                #endregion
            }
        }
        private void MergetListData()
        {
            try
            {
                setUI.Merge(setENV);
                setUI.Merge(setDISPO);
                setUI.Merge(setVKPOS);
                setUI.Merge(setUIWAART);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion



        #region==========BEAM========================
        private void vwWASTKE_MoveToFirst()
        {
            try
            {
                dwWASTKE = ((DataRowView)vwWASTKE.View.CurrentItem).Row;
                vwBSSATZ.View.MoveCurrentToFirst();
                while (true)
                {
                    dwBSSATZ = ((DataRowView)vwBSSATZ.View.CurrentItem).Row;
                    if (dwBSSATZ["BSKART"].ToString() == dwWASTKE["WAKKART"].ToString())
                    {
                        break;
                    }
                    if (vwBSSATZ.View.IsCurrentAfterLast)
                    {
                        vwBSSATZ.View.MoveCurrentToFirst();
                        break;
                    }
                    vwBSSATZ.View.MoveCurrentToNext();
                }
                dwBHSATZ = null;
                dwBSFARB = null;
                var query1 = from xwBHSATZ in setUI.Tables["BHSATZ"].AsEnumerable()
                             where xwBHSATZ["BHKART"].ToString() == dwBSSATZ["BSKART"].ToString()
                             select xwBHSATZ;
                foreach (DataRow dwTemp in query1)
                {
                    dwBHSATZ = dwTemp;
                }
                var query2 = from xwBSFARB in setUI.Tables["BSFARB"].AsEnumerable()
                             where xwBSFARB["BSFKART"].ToString() == dwBHSATZ["BHKART"].ToString()
                             && xwBSFARB["BSFKVAR"].ToString() == dwBHSATZ["BHKVAR"].ToString()
                             select xwBSFARB;
                foreach (DataRow dwTemp in query2)
                {
                    dwBSFARB = dwTemp;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void dlgWASTKE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwWASTKE = drv.Row;
                }
                vwWASTKE_MoveToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgBASTKE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwBASTKE = drv.Row;
                    vwBSSATZ.View.MoveCurrentToFirst();
                    while (true)
                    {
                        dwBSSATZ = ((DataRowView)vwBSSATZ.View.CurrentItem).Row;
                        if (dwBSSATZ["BSKART"].ToString() == dwBASTKE["BAKKART"].ToString())
                        {
                            break;
                        }
                        if (vwBSSATZ.View.IsCurrentAfterLast)
                        {
                            vwBSSATZ.View.MoveCurrentToFirst();
                            break;
                        }
                        vwBSSATZ.View.MoveCurrentToNext();
                    }
                }
                dwBHSATZ = null;
                dwBSFARB = null;
                var query1 = from xwBHSATZ in setUI.Tables["BHSATZ"].AsEnumerable()
                             where xwBHSATZ["BHKART"].ToString() == dwBSSATZ["BSKART"].ToString()
                             select xwBHSATZ;
                foreach (DataRow dwTemp in query1)
                {
                    dwBHSATZ = dwTemp;
                }
                var query2 = from xwBSFARB in setUI.Tables["BSFARB"].AsEnumerable()
                             where xwBSFARB["BSFKART"].ToString() == dwBHSATZ["BHKART"].ToString()
                             && xwBSFARB["BSFKVAR"].ToString() == dwBHSATZ["BHKVAR"].ToString()
                             select xwBSFARB;
                foreach (DataRow dwTemp in query2)
                {
                    dwBSFARB = dwTemp;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnNewWASTKE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRow dwBSFARB = setUI.BSFARB.NewRow();
                if (dwWAFARBEN == null)
                    return;
                int SizingBeamYarnEnds = System.Convert.ToInt16(this.txtTTENDS.Text.ToString());
                int numWbeam = System.Convert.ToInt16(this.txtNOBEAM.Text.ToString());
                if (SizingBeamYarnEnds == 0)
                {
                    MessageBox.Show("请输入织轴总头份，必需>0");
                    return;
                }
                if (numWbeam == 0)
                {
                    MessageBox.Show("请输入经轴个数，必需>0");
                    return;
                }
                bool IsSectionWarp = true; ;
                if (this.IsSectionBeam.IsChecked == true)
                {
                    IsSectionWarp = false;
                }
                DataSet setResult = m_Data.NewWEAVINGBeam(setUI, dwWAFARBEN, IsSectionWarp, SizingBeamYarnEnds, numWbeam);

                foreach (DataTable tbTemp in setResult.Tables)
                {
                    string TBName = tbTemp.TableName;
                    if (setUI.Tables.Contains(TBName))
                    {
                        setUI.Tables[TBName].Clear();
                    }
                }
                setUI.Merge(setResult);
                vwWASTKE.View.MoveCurrentToFirst();
                vwWASTKE_MoveToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void txtBAGKGNET_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                WarpMasterData m_WM = new WarpMasterData(OracleDB, proxy);
                dwBSSATZ = ((DataRowView)vwBSSATZ.View.CurrentItem).Row;
                dwBASTGA = ((DataRowView)vwBASTGA.View.CurrentItem).Row;
                dwBASTGA["BAGGART"] = m_WM.Left(txtGART.Text + "                               ", 20);
                dwBASTGA["BAGGVAR"] = m_WM.Left(txtGVAR.Text + "                               ", 3);
                m_WM.ComputeWarpQuantity(dwBSSATZ, ref dwBASTGA);
                DataTable dtBastga = setUI.BASTGA;
                var query = from dwBastga in dtBastga.AsEnumerable()
                            group new { dwBastga } by new
                            {
                                BAGGART = dwBastga["BAGGART"],
                                BAGGVAR = dwBastga["BAGGVAR"],
                                BAGGFARBE = dwBastga["BAGGFARBE"]
                            }
                                into grp
                                select new
                                {
                                    grp.Key.BAGGART,
                                    grp.Key.BAGGVAR,
                                    grp.Key.BAGGFARBE,
                                    sumBAGKGNET = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGNET")) / 1000,
                                    sumBAGKGBRUT = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGBRUT")) / 1000,
                                    sumBAGFDZ = grp.Sum(g => g.dwBastga.Field<decimal>("BAGFDZ")),
                                    sumBeam = grp.Count()
                                };
                dlgSumBASTGA.DataContext = query;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        #region==========YARN========================
        private void dlgBASTGA_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    isWarpYarn = true;
                    drv = e.AddedItems[0] as DataRowView;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void dlgWASTGA_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    isWarpYarn = false;
                    drv = e.AddedItems[0] as DataRowView;
                    dwWASTGA = drv.Row;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void cmbGSDAT_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ComboBox cb = sender as ComboBox;
                if (cb.SelectedItem == null)
                    return;
                DataRow dwTemp = ((DataRowView)cb.SelectedItem).Row;
                //cmbGSDAT.Text = dwTemp["GSART"].ToString();
                txtGVAR.Text = dwTemp["GSVAR"].ToString();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void dlgGSDAT_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwGSDAT;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwGSDAT = drv.Row;
                    if (isWarpYarn)
                    {

                        txtGVAR.Text = dwGSDAT["GSVAR"].ToString();
                        txtGART.Text = dwGSDAT["GSART"].ToString();

                        txtGVAR.Focus();
                        txtGART.Focus();
                        txtGVAR.Focus();
                        txtBAGKGNET.Focus();
                        return;
                    }
                    txtFGVAR.Text = dwGSDAT["GSVAR"].ToString();
                    txtFGART.Text = dwGSDAT["GSART"].ToString();
                    txtFGVAR.Focus();
                    txtFGART.Focus();
                    txtFGVAR.Focus();
                    txtWAGKGNET.Focus();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnNewWYarn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                isWarpYarn = true;
                if (dwBSSATZ == null)
                    return;
                if (dwBHSATZ == null)
                    return;
                if (dwBSFARB == null)
                    return;

                PatternMasterData m_PD = new PatternMasterData(OracleDB, proxy);
                DataSet setResult = new DataSet();
                DataRow dwPattern = null;
                dwPattern = m_PD.tbPattern.NewRow();
                dwPattern["PatternTable"] = "WTSAER";
                dwPattern["PatternType"] = "WarpPattern";
                dwPattern["PatternKeyName"] = "WTBSRFNR";
                dwPattern["PatternKey"] = m_PD.Left(cmbWBPattern.Text.Trim() + "                  ", 15);
                setResult = m_PD.GetPatternMaster(setResult, dwPattern);
                setUI.Tables["BASTGA"].Clear();
                if (setResult.Tables.Count == 0)
                {
                    return;
                }
                int pos = 1;
                string WTBGARN, WTBFDZ;
                string YARN;
                double WTBSRAPP = 1.0;
                dwBSSATZ = ((DataRowView)vwBSSATZ.View.CurrentItem).Row;
                int Ends, iWTBFDZ, BSFDZ = System.Convert.ToInt16(dwBSSATZ["BSFDZ"].ToString());
                foreach (DataRow dwWTSAER in setResult.Tables["WTSAER"].Rows)
                {
                    WTBSRAPP = System.Convert.ToDouble(dwWTSAER["WTBSRAPP"].ToString());
                    for (int yai = 1; yai < 25; yai++)
                    {
                        WTBGARN = "WTBGARN" + yai;
                        WTBFDZ = "WTBFDZ" + yai;
                        YARN = dwWTSAER[WTBGARN].ToString();
                        iWTBFDZ = System.Convert.ToInt16(dwWTSAER[WTBFDZ].ToString());
                        Ends = System.Convert.ToInt16(BSFDZ * iWTBFDZ / WTBSRAPP);
                        if (YARN.Trim() == "")
                            continue;
                        dwBASTGA = setUI.Tables["BASTGA"].NewRow();
                        dwBASTGA["BAGMANDANT"] = dwBSFARB["BSFMANDANT"];
                        dwBASTGA["BAGART"] = dwBSFARB["BSFKART"];
                        dwBASTGA["BAGVAR"] = dwBSFARB["BSFKVAR"];
                        dwBASTGA["BAGFARBE"] = dwBSFARB["BSFKFARBE"];
                        dwBASTGA["BAGPOS"] = m_PD.Right("00" + pos, 2);
                        dwBASTGA["BAGGART"] = "";
                        dwBASTGA["BAGGVAR"] = "";
                        dwBASTGA["BAGGFARBE"] = "          ";
                        dwBASTGA["BAGFDZ"] = Ends;
                        dwBASTGA["BAGABF"] = "0";
                        dwBASTGA["BAGKGNET"] = 0;
                        dwBASTGA["BAGKGBRUT"] = 0;
                        var query = from rowBASTGA in setUI.BASTGA
                                    where rowBASTGA["BAGMANDANT"].ToString() == dwBASTGA["BAGMANDANT"].ToString()
                                    && rowBASTGA["BAGART"].ToString() == dwBASTGA["BAGART"].ToString()
                                    && rowBASTGA["BAGVAR"].ToString() == dwBASTGA["BAGVAR"].ToString()
                                    && rowBASTGA["BAGFARBE"].ToString() == dwBASTGA["BAGFARBE"].ToString()
                                    && rowBASTGA["BAGPOS"].ToString() == dwBASTGA["BAGPOS"].ToString()
                                    select rowBASTGA;
                        if (query.AsDataView().Count == 0)
                        {
                            setUI.Tables["BASTGA"].Rows.Add(dwBASTGA);
                        }
                        else
                        {
                            foreach (DataRow dwTemp in query)
                            {
                                dwTemp.ItemArray = dwBASTGA.ItemArray;
                            }
                        }
                        pos++;
                    }
                }
                setUI.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnSaveWYarn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_Data.SaveBASTGA(setUI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnNewFYarn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                isWarpYarn = false;
                PatternMasterData m_PD = new PatternMasterData(OracleDB, proxy);
                DataSet setResult = new DataSet();
                DataRow dwPattern = null;
                dwPattern = m_PD.tbPattern.NewRow();
                dwPattern["PatternTable"] = "WTSCHUSS";
                dwPattern["PatternType"] = "WeftPattern";
                dwPattern["PatternKeyName"] = "WTSSFLGNR";
                dwPattern["PatternKey"] = m_PD.Left(cmbWFPattern.Text.Trim() + "                  ", 15);
                setResult = m_PD.GetPatternMaster(setResult, dwPattern);
                setUI.Tables["WASTGA"].Clear();
                int pos = 1;
                string WTBGARN, WTBFDZ;
                string YARN, Ends;
                GreigeMasterReport m_GR = new GreigeMasterReport();
                Queue WRWRQueue = new Queue();
                Array WR = Array.CreateInstance(typeof(Int32), 4, 698);
                Array WM = Array.CreateInstance(typeof(Int32), 22, 698);
                foreach (DataRow dwWTSCHUSS in setResult.Tables["WTSCHUSS"].Rows)
                {
                    WRWRQueue.Enqueue(WR);
                    WRWRQueue.Enqueue(WM);
                    WRWRQueue = m_GR.HandleWTSSCHUSS(WRWRQueue, dwWTSCHUSS, "F_P", "F_R");
                    WR = (Array)WRWRQueue.Dequeue();
                    WM = (Array)WRWRQueue.Dequeue();
                }
                int yai = 0;
                if (dwWAFARBEN == null)
                    return;
                int t = 0;
                int t1 = 0;
                int t2 = 0;
                int t3 = 0;
                for (int x = 0; x < 22; x++)
                {
                    yai = 0;
                    for (int y = 0; y < 698; y++)
                    {
                        t = (int)WR.GetValue(0, y);
                        t1 = (int)WR.GetValue(1, y);
                        t2 = (int)WR.GetValue(2, y);
                        t3 = (int)WR.GetValue(3, y);
                        if (t < 0)
                        {
                            t = -t;
                        }
                        else if (t == 0)
                        {
                            t = 1;
                        }
                        if (t1 < 0)
                        {
                            t1 = -t1;
                        }
                        else if (t1 == 0)
                        {
                            t1 = 1;
                        }
                        if (t2 < 0)
                        {
                            t2 = -t2;
                        }
                        else if (t2 == 0)
                        {
                            t2 = 1;
                        }
                        if (t3 < 0)
                        {
                            t3 = -t3;
                        }
                        else if (t3 == 0)
                        {
                            t3 = 1;
                        }
                        yai = yai + (int)WM.GetValue(x, y) * t * t1 * t2 * t3;

                    }
                    if (yai > 0)
                    {
                        dwWASTGA = setUI.Tables["WASTGA"].NewRow();
                        dwWASTGA["WAGMANDANT"] = dwWAFARBEN["WAFMANDANT"];
                        dwWASTGA["WAGART"] = dwWAFARBEN["WAFART"];
                        dwWASTGA["WAGVAR"] = dwWAFARBEN["WAFVAR"];
                        dwWASTGA["WAGWCOL"] = dwWAFARBEN["WAFZUSATZ"];
                        dwWASTGA["WAGPOS"] = "00";
                        dwWASTGA["WAGFOLGEPOS"] = pos;
                        dwWASTGA["WAGGART"] = "";
                        dwWASTGA["WAGGVAR"] = "";
                        dwWASTGA["WAGGFARBE"] = "          ";
                        dwWASTGA["WAGFDZ"] = yai;
                        dwWASTGA["WAGABF"] = "0";
                        dwWASTGA["WAGKGNET"] = 0;
                        dwWASTGA["WAGKGBRUT"] = 0;
                        var query = from rowWASTGA in setUI.WASTGA
                                    where rowWASTGA["WAGMANDANT"].ToString() == dwWASTGA["WAGMANDANT"].ToString()
                                    && rowWASTGA["WAGART"].ToString() == dwWASTGA["WAGART"].ToString()
                                    && rowWASTGA["WAGVAR"].ToString() == dwWASTGA["WAGVAR"].ToString()
                                    && rowWASTGA["WAGWCOL"].ToString() == dwWASTGA["WAGWCOL"].ToString()
                                    && rowWASTGA["WAGPOS"].ToString() == dwWASTGA["WAGPOS"].ToString()
                                    && rowWASTGA["WAGFOLGEPOS"].ToString() == dwWASTGA["WAGFOLGEPOS"].ToString()
                                    select rowWASTGA;
                        if (query.AsDataView().Count == 0)
                        {
                            setUI.Tables["WASTGA"].Rows.Add(dwWASTGA);
                        }
                        else
                        {
                            foreach (DataRow dwTemp in query)
                            {
                                dwTemp.ItemArray = dwWASTGA.ItemArray;
                            }
                        }

                        pos++;
                    }
                }
                setUI.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnSaveFYarn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_Data.SaveWASTGA(setUI.Tables["WASTGA"]);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        #region==========FABRIC======================

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        public void GenerateReport(string PrintType)
        {
            try
            {
                Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

                // check to kill the right process
                foreach (Process ExcelProcess in AllProcesses)
                {
                    ExcelProcess.Kill();
                }
                ArticleTechnic m_AT = new ArticleTechnic();
                m_AT.TxtArticleNum.Text = m_Data.Left(txtART.Text.Trim() + "                                            ", 20);
                m_AT.TxtArticleVar.Text = m_Data.Left(txtVAR.Text.Trim() + "   ", 2);
                m_AT.TxtColor.Text = color;
                m_AT.TxtFinish.Text = finish;
                m_AT.IsDirectlyCall = false;
                dicParamater["blBuildCellMastrix"] = true;
                m_AT.SetUp(dicParamater);
                m_AT.InitDataBinding();
                m_AT.GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void dlgBSFARB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwBSFARB = drv.Row;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgARTTECH_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwWASTAMM = drv.Row;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void cmbDRAFT_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ComboBox cb = sender as ComboBox;
                if (cb.SelectedItem == null)
                    return;
                int numDraft = System.Convert.ToInt16(((ComboBoxItem)cb.SelectedItem).Content.ToString());
                DataGridTextColumn cDFX = null, cPGX = null;
                string cDFn = "cDF", cPGn = "";
                for (int i = 1; i <= 16; i++)
                {
                    cDFn = "cDF" + m_Data.Right("00" + i, 2);
                    cPGn = "cPG" + m_Data.Right("00" + i, 2);
                    cDFX = (DataGridTextColumn)this.FindName(cDFn);
                    cPGX = (DataGridTextColumn)this.FindName(cPGn);
                    if (cDFX == null)
                        continue;
                    if (cPGX == null)
                        continue;
                    cDFX.Visibility = System.Windows.Visibility.Hidden;
                    cPGX.Visibility = System.Windows.Visibility.Hidden;
                    if (i <= numDraft)
                    {
                        cDFX.Visibility = System.Windows.Visibility.Visible;
                        cPGX.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {

                    }
                }
                setUI.DRAFTING.Clear();
                setUI.PEGPLAN.Clear();
                setUI.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnSaveWEAVE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ART = this.txtDRAFTING.Text.ToString();
                string VAR = "";
                int FN = 0;
                string temp = "";
                tbDRAFTING.AcceptChanges();
                tbPEGPLAN.AcceptChanges();
                foreach (DataRow dwTemp in tbDRAFTING.Rows)
                {
                    dwTemp["DFAART"] = ART;
                    FN++;
                    dwTemp["FN"] = FN.ToString();
                    dwTemp["DFADES"] = txtWTPBEZ.Text;
                }
                var queryX = from DataRow dwTemp in setWEAVE.Tables["DRAFTING"].AsEnumerable()
                             where dwTemp["DFAART"].ToString().Trim() == ART.Trim()
                             select dwTemp;
                List<DataRow> deletedRows = new List<DataRow>();
                foreach (DataRow dwTemp in queryX)
                {
                    deletedRows.Add(dwTemp);
                }
                foreach (DataRow dwTemp in deletedRows)
                {
                    dwTemp.Delete();
                }
                setWEAVE.Tables["DRAFTING"].AcceptChanges();
                setWEAVE.AcceptChanges();
                setWEAVE.Merge(tbDRAFTING);
                ART = txtPEGPLAN.Text.ToString();
                FN = 0;
                foreach (DataRow dwTemp in tbPEGPLAN.Rows)
                {
                    dwTemp["PGAART"] = ART;
                    FN++;
                    dwTemp["FN"] = FN.ToString();
                }
                queryX = from DataRow dwTemp in setWEAVE.Tables["PEGPLAN"].AsEnumerable()
                         where dwTemp["PGAART"].ToString().Trim() == ART.Trim()
                         select dwTemp;
                deletedRows = new List<DataRow>();
                foreach (DataRow dwTemp in queryX)
                {
                    deletedRows.Add(dwTemp);
                }
                foreach (DataRow dwTemp in deletedRows)
                {
                    dwTemp.Delete();
                }
                setWEAVE.Tables["PEGPLAN"].AcceptChanges();
                setWEAVE.AcceptChanges();
                setWEAVE.Merge(tbPEGPLAN);
                m_Data.SaveSetWEAVE(setWEAVE);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void dlgWEAVE_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                DataGrid dg = sender as DataGrid;

                if (dg.Name == "dlgPEGPLAN")
                {

                    //txtPEGPLANWEFT.Text = tbPEGPLAN.Rows.Count.ToString();

                }
                else
                {
                    //txtMaxDRAFT.Text =tbDRAFTING.Rows.Count.ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnQueryWEAVE_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PEGAART = txtPEGPLAN.Text.ToString();
                string DFAART = txtDRAFTING.Text.ToString();
                setUI.DRAFTING.Clear();
                setUI.PEGPLAN.Clear();
                DataSet setResult = m_Data.QueryWeave(setWEAVE, PEGAART, DFAART);
                if (setResult.Tables[0].Rows.Count == 0)
                {
                    DataRow dwPEGPLAN = tbPEGPLAN.NewRow();
                    dwPEGPLAN["PGAART"] = PEGAART;
                    dwPEGPLAN["FN"] = 1;
                    tbPEGPLAN.Rows.Add(dwPEGPLAN);

                    DataRow dwDRFAFT = tbDRAFTING.NewRow();
                    dwDRFAFT["DFAART"] = PEGAART;
                    dwDRFAFT["FN"] = 1;
                    tbDRAFTING.Rows.Add(dwDRFAFT);
                    setUI.Merge(tbDRAFTING);
                    setUI.Merge(tbPEGPLAN);
                }
                else
                {
                    setUI.Merge(setResult);
                }
                vwDRAFTING.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void txtWTPBEZ_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                /*
                string WTPPASSNR = txtDRAFTING.Text;
                if (WTPPASSNR.Trim() == "")
                    return;
                TextBox t = sender as TextBox;
                GreigeMasterData m_GM = new GreigeMasterData(OracleDB, proxy, UserID);
                DataRow dwWTPASS = setUI.WTPASS.NewWTPASSRow();
              
                dwWTPASS["WTPPASSNR"] = WTPPASSNR;
                string WTPBEZ=t.Text+"                            ";
                WTPBEZ=m_GM.ConvertGB2312To8859P1(WTPBEZ);
                dwWTPASS["WTPBEZ"] = m_GM.Left(WTPBEZ, 30); 
                m_GM.UpdateWTPASS(dwWTPASS);
                */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void cmbWBPattern_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cmbWFPattern_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public void QueryWASTAMM()
        {

            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    dwWASTAMM = ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
                    DataRow dwART = tbUIWAART.NewRow();
                    dwART["WAART"] = dwWASTAMM["WAART"];
                    dwART["WAVAR"] = dwWASTAMM["WAVAR"];
                    dwART["WAFART"] = dwWASTAMM["WAART"];
                    dwART["WAFVAR"] = dwWASTAMM["WAVAR"];
                    dwART["WAFZUSATZ"] = m_Data.Left(m_Data.ConvertGB2312To8859P1(txtWAFZUSATZ.Text) + "             ", 10);
                    setResult = m_Data.GetData(dwART, color, finish);

                    #region
                    if (setResult.Tables["WTGEW"].Rows.Count == 0)
                    {
                        setResult.Merge(m_GM.NewWS22(dwWASTAMM));
                    }
                    if (setResult.Tables["WASTPASS"].Rows.Count == 0)
                    {
                        setResult.Merge(m_GM.NewWASTPASS(dwWASTAMM));
                    }

                    var query = from dwTemp in setResult.Tables["WAFARBEN"].AsEnumerable()
                                where dwTemp["WAFZUART"].ToString() == "C"
                                && dwTemp["WAFZUSATZ"].ToString().Trim() == color.Trim()
                                select dwTemp;
                    if (query.Count() == 0)
                    {
                        setResult.Merge(m_GM.NewWAFARBEN(dwWASTAMM, "C", color));
                    }

                    query = from dwTemp in setResult.Tables["WAFARBEN"].AsEnumerable()
                            where dwTemp["WAFZUART"].ToString() == "B"
                            && dwTemp["WAFZUSATZ"].ToString().Trim() == finish.Trim()
                            select dwTemp;
                    if (query.Count() == 0)
                    {
                        setResult.Merge(m_GM.NewWAFARBEN(dwWASTAMM, "B", finish));
                    }
                    #endregion

                    string proectedtable = "WASTAMM;WASTKE;GSDAT;WTSAER; WTSCHUSS;";
                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string TBName = tbTemp.TableName;
                        if (proectedtable.Contains(TBName + ";"))
                            continue;
                        if (setUI.Tables.Contains(TBName))
                        {
                            setUI.Tables[TBName].Clear();
                        }
                    }
                    setUI.Merge(setResult);
                    vwWTGEW.View.MoveCurrentToFirst();
                    vwWASTPASS.View.MoveCurrentToFirst();
                    string PGAART = txtPEGPLAN.Text.ToString();
                    string DFAART = txtDRAFTING.Text.ToString();
                    setResult = m_Data.QueryWeave(setWEAVE, PGAART, DFAART);
                    setUI.Merge(setResult);
                    vwDRAFTING.View.MoveCurrentToFirst();
                    EventDispatcher.Instance.RaiseStatus("品种信息已完全读取", StatusEventType.StopProgress);
                    btnRefreshABON_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear();
                setUI.AcceptChanges();
                MergetListData();
                #region===========GetWASTAMM=================
                dwUI = tbUIWAART.NewRow();
                dwUI["WAART"] = m_Data.Left(txtART.Text.Trim() + "                                            ", 20);
                dwUI["WAVAR"] = m_Data.Left(txtVAR.Text.Trim() + "                                            ", 2);
                dwUI["WANEUB"] = m_Data.UserID;
                dwUI["WANEUD"] = m_Data.WorkingDate;
                setResult = m_Data.QueryWASTAMM(dwUI);
                setUI.Merge(setResult);
                #endregion

                #region===========CreateWASTAMM=================
                if (setResult.Tables["WASTAMM"].Rows.Count == 0)
                {
                    m_Data.CreateWASTAMM(dwUI, ref setUI, color, finish);
                    btnQuery_Click(null, null);
                }
                #endregion

                #region============MAPUI=================
                vwWASTAMM.View.MoveCurrentToFirst();
                QueryWASTAMM();
                dwWASTAMM = ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
                dwWASTAMM["WAFFERTKZ"] = "E";
                dwWASTAMM["WARFERTKZ"] = "E";
                dwWASTAMM["WAGASTA"] = "V";
                dwWASTAMM["WAMASTA"] = "V";
                dwWASTAMM["WAKESTA"] = "V";
                dwWASTAMM["WAMATPRF"] = " ";
                dwWASTAMM["WABRTPRF"] = " ";
                dwWASTAMM["WASTKGP"] = " ";
                dwWASTAMM["WASCHLIP"] = " ";
                dwWASTAMM["WASCHLIA"] = " ";
                dwWASTAMM["WALIEFNR"] = "        ";
                dwWASTAMM["WAEG"] = "N";
                dwWASTAMM["WAGFKZ"] = "J";
                dwWASTAMM["WAFRBST"] = "C";

                dwWASTAMM.AcceptChanges();
                vwVKPOS.View.MoveCurrentToFirst();
                vwWDECK.View.MoveCurrentToFirst();
                vwWTGEW.View.MoveCurrentToFirst();
                vwWASTPASS.View.MoveCurrentToFirst();
                vwWASTKE.View.MoveCurrentToFirst();
                vwWASTKE_MoveToFirst();
                znWS.IsExpanded = true;
                znBS.IsExpanded = true;
                #endregion
                BlockUI(49, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void btnRefreshUIWAART_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUIWAART.Clear();
                string WAART = txtART.Text.Trim();
                DataSet setResult = m_Data.QueryWASTAMM(WAART);
                setUIWAART.Merge(setResult);
                BlockUI(39, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgUIWAART_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwGSDAT;
            DataRow dwUIWAART;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwUIWAART = drv.Row;
                    this.txtART.Text = dwUIWAART["WAART"].ToString();
                    this.txtVAR.Text = dwUIWAART["WAVAR"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgWTSAER_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwGSDAT;
            DataRow dwWTSAER;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwWTSAER = drv.Row;
                    this.cmbWBPattern.Text = dwWTSAER["WTBSRFNR"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void dlgWTSCHUSS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwGSDAT;
            DataRow dwWTSCHUSS;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwWTSCHUSS = drv.Row;
                    this.cmbWFPattern.Text = dwWTSCHUSS["WTSSFLGNR"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnSaveWASTAMM_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                DataSet setResult;
                if (m_Data.SaveWASTAMM(setUI, color, finish))
                {
                    //setUI.Clear();
                    btnGetVKPOS_Click(null, null);
                    btnQuery_Click(null, null);
                    btnSaveWEAVE_Click(null, null);
                    DataRow dwWASTAMM = null, dwWDECK = null;
                    if (setUI.Tables["WASTAMM"].Rows.Count > 0)
                    {
                        dwWASTAMM = setUI.Tables["WASTAMM"].Rows[0];
                        if (setUI.Tables["WDECK"].Rows.Count > 0)
                        {
                            dwWDECK = setUI.Tables["WDECK"].Rows[0];
                            m_GM.UpdateWDECK(dwWDECK, dwWASTAMM);
                        }
                    }
                    
                }
            }
        }

        private void cmbWABIND_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            DataRow dwGSDAT;
            DataRow dwWABIND;
            try
            {
                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    dwWABIND = drv.Row;
                    /*this.cmbWABIND.Text = dwWABIND["WABINDKEY"].ToString();
                    dwWASTAMM["WABIND"] = dwWABIND["WABINDKEY"];*/
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void txtWAGKGNET_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                GreigeMasterData m_GM = new GreigeMasterData(OracleDB, proxy);
                dwWASTAMM = ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
                dwWTGEW = ((DataRowView)vwWTGEW.View.CurrentItem).Row;
                dwWASTGA = ((DataRowView)vwWASTGA.View.CurrentItem).Row;
                dwWASTGA["WAGGART"] = m_GM.Left(txtFGART.Text + "                               ", 20);
                dwWASTGA["WAGGVAR"] = m_GM.Left(txtFGVAR.Text + "                               ", 3);
                m_GM.ComputeWeftQuantity(dwWASTAMM, dwWTGEW, ref dwWASTGA);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        #endregion

        #region==========AV95ON======================

        private void bgWorkerOn_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                EventDispatcher.Instance.RaiseStatus("完成创建订单网络", StatusEventType.StopProgress);
                UpdateByABON();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void bgWorkerOn_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                setUI.Merge(setUIABPOS);
                m_AB05.GetData(setUI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnInsertABON_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EventDispatcher.Instance.RaiseStatus("开始创建订单网络", StatusEventType.StartProgress);
                bgWorkerOn.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox t = sender as TextBox;
            t.SelectAll();
        }

        private void btnGetVKPOS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataRow dwVKPOS = setUI.VKPOS.NewRow();
                    dwVKPOS["VKPAUFKRS"] = m_Data.Right("  " + txtVKPAUFKRS.Text.Trim(), 2);
                    dwVKPOS["VKPAUFLFD"] = m_Data.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
                    dwVKPOS["VKPAUFPOS"] = m_Data.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
                    setVKPOS = m_Data.QueryVKPOS(dwVKPOS);                    
                    setUI.Clear();
                    setUI.AcceptChanges();
                    MergetListData();
                    vwVKPOS.View.MoveCurrentToFirst();
                    vwWDECK.View.MoveCurrentToFirst();
                    exVKPOS.IsExpanded = false;
                    dicParamater["User"] = UserID;
                    znVKPOS.SetUp(dicParamater);
                    znVKPOS.checkVK07 = false;
                    znVKPOS.TxtMANDANT.Text = "00";
                    znVKPOS.TxtUserID.Text = UserID;
                    if (vwVKPOS.View != null)
                    {
                        dwVKPOS = ((DataRowView)vwVKPOS.View.CurrentItem).Row;
                    }
                    znVKPOS.TxtKRS.Text = dwVKPOS["VKPAUFKRS"].ToString();
                    znVKPOS.TxtLFD.Text = dwVKPOS["VKPAUFLFD"].ToString();
                    znVKPOS.btnQuery_Click(null, null);
                }
                BlockUI(29, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnDATADISPO_Click(object sender, RoutedEventArgs e)
        {
            SETUPABON setupab = new SETUPABON();
            setupab.Show();
            //_Control.ActivateReportByDocType("SETUPABON");
        }

        private void btnRefreshABON_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tbUIABPOS.Clear();
                setUIABPOS.Clear();
                DataRow dwUIABPOS, dwVKPOS, dwWASTAMM, dwWASTKE, dwWASTGA, dwBASTGA, dwWDECK, dwdwWASTAMM;
                vwVKPOS.View.MoveCurrentToFirst();
                vwWDECK.View.MoveCurrentToFirst();
                vwWASTAMM.View.MoveCurrentToFirst();
                vwWASTKE.View.MoveCurrentToFirst();
                vwWASTGA.View.MoveCurrentToFirst();
                vwBASTGA.View.MoveCurrentToFirst();
                vwWASTAMM.View.MoveCurrentToFirst();
                dwWASTAMM = ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
                dwVKPOS = ((DataRowView)vwVKPOS.View.CurrentItem).Row;
                dwWDECK = ((DataRowView)vwWDECK.View.CurrentItem).Row;
                dwWASTKE = ((DataRowView)vwWASTKE.View.CurrentItem).Row;
                dwWASTGA = ((DataRowView)vwWASTGA.View.CurrentItem).Row;
                dwBASTGA = ((DataRowView)vwBASTGA.View.CurrentItem).Row;
                if (dwVKPOS == null
                    || dwWDECK == null
                    || dwWASTKE == null
                    || dwWASTGA == null
                    || dwBASTGA == null
                    )
                    return;
                DataRow dwDispo = setUI.Tables["DATADISPO"].Rows[0];
                #region============AB05====================
                dwUIABPOS = tbUIABPOS.NewRow();
                dwUIABPOS["ORDTYPE"] = "成品";
                string UM = "米";
                double QUANTITY;
                string DELIVERYDATE = "";
                dwWDECK = null;
                var query = from dwTemp in setUI.Tables["WDECK"].AsEnumerable()
                            where dwTemp["WDAUFKRS"].ToString() == dwVKPOS["VKPAUFKRS"].ToString()
                            & dwTemp["WDAUFLFD"].ToString() == dwVKPOS["VKPAUFLFD"].ToString()
                            & dwTemp["WDAUFPOS"].ToString() == dwVKPOS["VKPAUFPOS"].ToString()
                           & dwTemp["WDSA"].ToString() == "K"
                           & dwTemp["WDUPOS"].ToString() == "00"
                            select dwTemp;
                foreach (DataRow dwTemp in query)
                {
                    dwWDECK = dwTemp;
                }
                DELIVERYDATE = dwWDECK["WDDAT"].ToString();

                UM = dwVKPOS["VKPMEEH"].ToString();
                QUANTITY = System.Convert.ToDouble(dwVKPOS["VKPMENGE1"].ToString()) * 0.01;
                if (UM.Trim() == "YD")
                    QUANTITY = System.Convert.ToDouble(System.Math.Round(QUANTITY * 0.9144, 0));
                UM = "米";
                dwUIABPOS["UM"] = UM;
                dwUIABPOS["UNIT"] = 0;
                dwUIABPOS["QUANTITY"] = QUANTITY;
                dwUIABPOS["DELIVERYDATE"] = DELIVERYDATE;
                dwUIABPOS["LEADTIME"] =3;
                tbUIABPOS.Rows.Add(dwUIABPOS);
                #endregion
                #region============AB55====================
                dwUIABPOS = tbUIABPOS.NewRow();
                UM = "米";
                dwUIABPOS["ORDTYPE"] = "织造";
                dwUIABPOS["UM"] = UM;
                dwUIABPOS["UNIT"] = 0;
                DELIVERYDATE = m_GM.GetDateAddDays(DELIVERYDATE, 0 - 5);
                double WALAEAEND = System.Convert.ToDouble(dwWASTAMM["WALAEAEND"].ToString());
                QUANTITY = System.Convert.ToDouble(System.Math.Round(QUANTITY / (1 + WALAEAEND * 0.0001), 0));
                dwUIABPOS["QUANTITY"] = QUANTITY;
                dwUIABPOS["DELIVERYDATE"] = DELIVERYDATE;
                dwUIABPOS["LEADTIME"] =10;
                tbUIABPOS.Rows.Add(dwUIABPOS);

                #endregion
                #region============WEFTYARN====================

                #endregion
                #region============AB45====================
                dwUIABPOS = tbUIABPOS.NewRow();
                UM = "米";
                dwUIABPOS["ORDTYPE"] = "准备";
                dwUIABPOS["UM"] = UM;
                double GL = QUANTITY;
                double RT = System.Convert.ToDouble(dwWASTKE["WAKEAFAKTR"]);
                double BAMTR1 = System.Convert.ToDouble(System.Math.Round(GL * RT * 0.0001 * 10, 0));
                dwUIABPOS["QUANTITY"] = BAMTR1;
                dwUIABPOS["UNIT"] = System.Math.Ceiling(BAMTR1 / 2500);
                DELIVERYDATE = m_GM.GetDateAddDays(DELIVERYDATE, 0 - 15);
                dwUIABPOS["DELIVERYDATE"] = DELIVERYDATE;
                dwUIABPOS["LEADTIME"] =7;
                tbUIABPOS.Rows.Add(dwUIABPOS);

                #endregion
                #region============WARPYARN====================
                #endregion

                setUIABPOS.Merge(tbUIABPOS);
                setUIABPOS.Merge(tbWFGAAUF);
                setUIABPOS.Merge(tbWPGAAUF);
                dlgUIABPOS.DataContext = setUIABPOS;
                dlgWFGAAUF.DataContext = setUIABPOS;
                dlgWPGAAUF.DataContext = setUIABPOS;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void UpdateByABON()
        {
            try
            {
                setUIABPOS.Clear();
                tbWFGAAUF.Clear();
                tbUIABPOS.Clear();
                tbWPGAAUF.Clear();
                string ABPAUFART;
                string UM = "米";
                DataRow dwUIABPOS;
                dwUIABPOS = tbUIABPOS.NewRow();
                dwUIABPOS["UM"] = UM;

                #region============ABPOS====================
                foreach (DataRow dwABPOS in setUI.ABPOS)
                {
                    dwUIABPOS = tbUIABPOS.NewRow();
                    dwUIABPOS["UM"] = UM;
                    ABPAUFART = dwABPOS["ABPAUFART"].ToString();
                    dwUIABPOS["ORDNUM"] = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                    dwUIABPOS["DELIVERYDATE"] = dwABPOS["ABPLIEFDAT"];
                    DateTime ts1 = System.Convert.ToDateTime(dwABPOS["ABPENDDAT"].ToString().Substring(0,4)+ "-"
                    + dwABPOS["ABPENDDAT"].ToString().Substring(4,2) + "-"
                    + dwABPOS["ABPENDDAT"].ToString().Substring(6,2));

                    DateTime ts2 = System.Convert.ToDateTime(dwABPOS["ABPSTARTDAT"].ToString().Substring(0, 4) + "-"
                    + dwABPOS["ABPST4ARTDAT"].ToString().Substring(4, 2) + "-"
                    + dwABPOS["ABPSTARTDAT"].ToString().Substring(6, 2)); 

                    TimeSpan ts = ts1.Subtract(ts2).Duration();

                    string dateDiff = ts.Days.ToString() ;

                    dwUIABPOS["LEADTIME"] = ts.Days.ToString();

                    if (ABPAUFART == "VA")
                    {
                        dwUIABPOS["ORDTYPE"] = "成品";
                        dwUIABPOS["QUANTITY"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE2"].ToString()) * 0.01, 0);
                        dwUIABPOS["UNIT"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE8"].ToString()) * 0.01, 0);
                    }
                    if (ABPAUFART == "DI")
                    {
                        dwUIABPOS["ORDTYPE"] = "织造";
                        dwUIABPOS["QUANTITY"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE1"].ToString()) * 0.01, 0);
                        dwUIABPOS["UNIT"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE8"].ToString()) * 0.01, 0);
                    }
                    if (ABPAUFART == "VW")
                    {
                        dwUIABPOS["ORDTYPE"] = "准备";
                        dwUIABPOS["QUANTITY"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE1"].ToString()) * 0.01, 0);
                        dwUIABPOS["UNIT"] = System.Math.Round(System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE8"].ToString()) * 0.01, 0);
                    }
                    tbUIABPOS.Rows.Add(dwUIABPOS);
                }
                #endregion
                #region============WEFTYARN====================
                string GASA = "";
                DataRow dwWFGAAUF = tbWFGAAUF.NewRow();
                DataRow dwWPGAAUF = tbWPGAAUF.NewRow();
                foreach (DataRow dwGAAUF in setUI.GAAUF.Rows)
                {
                    GASA = dwGAAUF["GASA"].ToString();
                    if (GASA == "732")
                    {
                        dwWFGAAUF = tbWFGAAUF.NewRow();
                        dwWFGAAUF["UM"] = "KG";
                        dwWFGAAUF["YARN"] = dwGAAUF["GAART"].ToString() + dwGAAUF["GAVAR"].ToString();
                        dwWFGAAUF["QUANTITY"] = System.Convert.ToDouble(dwGAAUF["GAKG1"].ToString()) * 0.01;
                        tbWFGAAUF.Rows.Add(dwWFGAAUF);
                    }
                    if (GASA == "723")
                    {
                        dwWPGAAUF = tbWPGAAUF.NewRow();
                        dwWPGAAUF["UM"] = "KG";
                        dwWPGAAUF["YARN"] = dwGAAUF["GAART"].ToString() + dwGAAUF["GAVAR"].ToString();
                        dwWPGAAUF["QUANTITY"] = System.Convert.ToDouble(dwGAAUF["GAKG1"].ToString()) * 0.01;
                        tbWPGAAUF.Rows.Add(dwWPGAAUF);
                    }
                }

                #endregion
                #region============WARPYARN====================
                #endregion
                setUIABPOS.Merge(tbUIABPOS);
                setUIABPOS.Merge(tbWPGAAUF);
                setUIABPOS.Merge(tbWFGAAUF);
                dlgUIABPOS.DataContext = setUIABPOS;
                dlgWFGAAUF.DataContext = setUIABPOS;
                dlgWPGAAUF.DataContext = setUIABPOS;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion


    }
}
